//
//  DHMainController.m
//  精品酒店
//
//  Created by DIANGHONG on 2018/5/7.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "DHMainController.h"

@interface DHMainController ()<UITabBarControllerDelegate>

@property(nonatomic ,strong)NSArray *controllersInfo; //  子控制器信息数组



@end

@implementation DHMainController

- (NSArray *)controllersInfo{
    if (!_controllersInfo) {
        _controllersInfo = @[@{@"clsName":@"DHHomeController",@"title":@"首页",@"imageName":@"首页",@"isShouldLoginShow":@0},
                             @{@"clsName":@"DHOrderController",@"title":@"订单",@"imageName":@"订单",@"isShouldLoginShow":@1},
                             @{@"clsName":@"DHMineViewController",@"title":@"我的",@"imageName":@"我的",@"isShouldLoginShow":@1},
                             @{@"clsName":@"DHServiceController",@"title":@"客服",@"imageName":@"客服",@"isShouldLoginShow":@0},
                             ];
    }
    return _controllersInfo;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //创建子控制器
    [self setUpUIWithcontrollersInfoArray:self.controllersInfo];
    //[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(relogin) name:kReLoginNotification object:nil];
    //self.isLogout = NO;
    //DHNetworkManager.shareManager.userAccount.islogout = false;
}

- (void)relogin
{
   //  DHNetworkManager.shareManager.userAccount.islogout = true;
    
}
- (void)dealloc
{
    [NSNotificationCenter.defaultCenter removeObserver:self];
}
- (void)setUpUIWithcontrollersInfoArray:(NSArray *)controllersInfo {
    
    //创建可变数组
    NSMutableArray *controllers = [NSMutableArray arrayWithCapacity:2];
    
    //遍历数组创建控制器
    for (NSDictionary *dict in controllersInfo) {
        UIViewController *vc = [self creatChildrenControllerWithDict:dict];
        
        [controllers addObject:vc];
    }
    [self setViewControllers:controllers.copy];
    
   // self.selectedIndex = 1;
   
   
 //   if (DHNetworkManager.shareManager.userAccount.islogout) {
//
//        DHNetworkManager.shareManager.userAccount.islogout = NO;
//    }else{
//        self.selectedIndex = 0;
//    }
    
}


/**
 取一个子控制器
 
 @param dict 控制器信息字典
 @return 子控制器
 */
- (UIViewController *)creatChildrenControllerWithDict:(NSDictionary *)dict{
    
    //获取类名称
    NSString *clsName = dict[@"clsName"];
    NSString *title = dict[@"title"];
    NSString *imageName = dict[@"imageName"];
    NSNumber *isShouldLoginToshow = dict[@"isShouldLoginShow"];
    //创建控制器
    DHBaseController *vc = [[NSClassFromString(clsName) alloc] init];
    vc.ShouldLoginToShow = isShouldLoginToshow.boolValue;
    //创建nav控制器
    DHNavController *nav = [[DHNavController alloc] initWithRootViewController:vc];
    
   // vc.view.backgroundColor = [UIColor randomColor];
    //设置控制器属性
    vc.title = [vc isMemberOfClass:[DHHomeController class]] ? nil : title;
    //vc.tabBarItem.image = [UIImage createNewImageWithColor:[UIImage imageNamed:imageName] multiple:0];
    vc.tabBarItem.image = [UIImage createNewImage:[UIImage imageNamed:imageName] newSize:CGSizeMake(22, 22)];
   // vc.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 5, -5, 5);
    [vc.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} forState:0];
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor orangeColor]} forState:UIControlStateSelected];
    
    [self.tabBar setTintColor:[UIColor orangeColor]];
    
    return nav;
}

@end
